<?php
/*
Primary Author: Derek Shue
Secondary: Alex Arey
Documentation: Cheng Yi
Updated: 12/10/2011
Module Description: The Course module adds/finds/updates/deletes Courses from the database.
*/
class Application_Model_DbTable_Courses extends Zend_Db_Table_Abstract
{

protected $_name = 'COURSES';

public function getCourse($where)
    {
        $row = $this->fetchRow($where);
        if (!$row) {
            return null;
        }
        return $row->toArray();
    }

    public function addCourse($coursenumber,$coursename, $description, $department, $teacherid, $term, $syllabus)
    {
		$cid = $this->getNextKey();
        $data = array(
			'CID'=> $cid,
			'COURSENUMBER'=> $coursenumber,
			'COURSENAME'=> $coursename,
			'DEPARTMENT'=> $department,
			'DESCRIPTION' => $description,
			'TEACHERID'=> $teacherid,
			'TERM'=> $term,
			'SYLLABUS'=> $syllabus,
			//'PLACEMENTLIST'=> $placementlist,
			//'YEAR' => $year,

        );
		
        $this->insert($data);
    }

    public function updateCourse($cid,$coursenumber,$coursename, $description, $department, $teacherid,$term,$syllabus)
    {
        $data = array(
			'COURSENUMBER'=> $coursenumber,
			'COURSENAME'=> $coursename,
			'DESCRIPTION' => $description,
			'DEPARTMENT' => $department,
			'TEACHERID'=> $teacherid,
			'TERM'=> $term,
			'SYLLABUS'=> $syllabus,
			//'PLACEMENTLIST'=> $placementlist,
			//'YEAR' => $year,

        );
        $this->update($data, 'CID = '. (int)$cid);
    }
	
	public function getCourses($where){
	
	$sqlquery = $this->select()->where($where);
	$results = $this->fetchAll($sqlquery);
	
	return $results;	
	
	}
	

	public function deleteCourse($where)
    {
        $this->delete($where);
    }

		
	public function getNextKey(){
			$sqlquery = $this->select()->from($this->_name, array("MAX(CID)"));
			$insertrow = $this->fetchRow($sqlquery)->toArray(); 
			return $insertrow["MAX(CID)"] + 1;
	
	}
}
	
   ?>